package org.discala.samples.complex.chef

import appliance.Appliance
import scala.util.Random
import org.discala.samples.complex.domain.{Meal, Recipe}
import org.discala.samples.complex.domain.Duration._
import org.discala.samples.complex.domain.PreparationMethod.PreparationMethod

/**
 * Any similarities with any real life chefs are purely coincidental!
 *
 * @author Chris Turner
 */
private[chef] trait ChefComponent {

  this: AnyRef { val vessel: () => Vessel
                 val appliance: (PreparationMethod) => Appliance }  =>

  val chef: () => Chef

  class HestonRamsey extends Chef {

    private [this] val random = new Random

    def cookMeal(recipe: Recipe) = {
      val vessel = mixIngredients(recipe)
      val duration = selectDuration()
      val currentAppliance = appliance(recipe.preparationMethod)

      val description = currentAppliance.describeResult(duration) + vessel.describeContents +
                        ". It exudes " + vessel.describeAroma +
                        " and has " + vessel.describeTaste
      new Meal(recipe, description)
    }

    private[this] def mixIngredients(recipe: Recipe) = {
      val currentVessel = vessel()
      recipe.eachIngredient {currentVessel << _}
      currentVessel
    }

    private[this] def selectDuration() = {
      random.synchronized {
        random.nextInt(3) match {
          case 0 => Short
          case 1 => Medium
          case _ => Long
        }
      }
    }
  }
}
